package com.bor.gb.core.services;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

/**
 * Created by bor on 10/26/14.
 */
@Component
public class MailService {

    private static final Log log = LogFactory.getLog(MailService.class);

    @Autowired
    private MailSender mailSender;

    @Autowired
    private SimpleMailMessage templateMessage;

    @Value("${email.confirmation.url}")
    private String activationUrlTmpl;


    public void sendMessage(String email, String userId, String lockKey) {

        SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);

        String url = String.format(activationUrlTmpl,userId, lockKey);

        msg.setTo(email);
        msg.setText("Activate: " + url);

        try {
            this.mailSender.send(msg);
        } catch (MailException ex) {
            log.error("Error occurred during email sending to user " + email, ex);
        }

    }
}
